package com.itfreer.arcgis.draw;

import com.esri.arcgisruntime.geometry.Geometry;
import com.esri.arcgisruntime.geometry.Point;
import com.esri.arcgisruntime.layers.FeatureLayer;
import com.esri.arcgisruntime.mapping.view.MapView;
import com.esri.arcgisruntime.mapping.view.SketchCreationMode;
import com.esri.arcgisruntime.mapping.view.SketchEditor;
import com.esri.arcgisruntime.mapping.view.SketchStyle;
import com.esri.arcgisruntime.mapping.view.SketchVertex;
import com.esri.arcgisruntime.symbology.SimpleLineSymbol;
import com.esri.arcgisruntime.symbology.SimpleMarkerSymbol;
import com.itfreer.arcgis.utils.GeometryUtils;

import java.util.List;

/**
 * 绘制基础类
 */
public abstract class DrawTool
{
    protected SketchEditor sketchEditor;
    protected  DrawToolType drawToolType;
    FeatureLayer eidtFeatueLayer;

    protected FeatureLayer getEidtFeatueLayer()
    {
        return eidtFeatueLayer;
    }

    protected void setEidtFeatueLayer(FeatureLayer eidtFeatueLayer)
    {
        this.eidtFeatueLayer = eidtFeatueLayer;
    }

    public DrawTool(SketchEditor sketchEditor,DrawToolType drawToolType)
    {
        DrawToolManager.setCurrentTool(this);
        this.sketchEditor = sketchEditor;
        this.drawToolType =drawToolType;
    }

    /**
     * 绘制基础类,执行绘制方法
     * @param mapView
     * @param geometry
     */
    public void Draw(MapView mapView, Geometry geometry)
    {
        SketchStyle mainSketchStyle = new SketchStyle();
        SimpleMarkerSymbol mVertexSymbol = new SimpleMarkerSymbol(com.esri.arcgisruntime.symbology.SimpleMarkerSymbol.Style.SQUARE, -65536, 7.0F);
        SimpleMarkerSymbol mSelectedVertexSymbol = new SimpleMarkerSymbol(com.esri.arcgisruntime.symbology.SimpleMarkerSymbol.Style.SQUARE, -65536, 7.0F);

        SimpleMarkerSymbol mMidVertexSymbol = new SimpleMarkerSymbol(com.esri.arcgisruntime.symbology.SimpleMarkerSymbol.Style.CIRCLE, -1, 5.0F);
        SimpleMarkerSymbol mSelectedMidVertexSymbol = new SimpleMarkerSymbol(com.esri.arcgisruntime.symbology.SimpleMarkerSymbol.Style.CIRCLE, -1, 5.0F);

        mainSketchStyle.setVertexSymbol(mVertexSymbol);
        mainSketchStyle.setSelectedVertexSymbol(mSelectedVertexSymbol);

        mainSketchStyle.setMidVertexSymbol(mMidVertexSymbol);
        mainSketchStyle.setSelectedMidVertexSymbol(mSelectedMidVertexSymbol);

        sketchEditor.setSketchStyle(mainSketchStyle);
        mapView.setSketchEditor(sketchEditor);
        SketchCreationMode mode = getSketchMode(drawToolType);
        if(mode !=null)
        {
            if (geometry != null && !geometry.isEmpty())
                sketchEditor.start(geometry, mode);
            else
                sketchEditor.start(mode);
        }
    }

    /**
     * 清除绘制
     */
    public void Clear()
    {
        if(sketchEditor !=null)
            sketchEditor.stop();
    }

    /**
     * 更新图形
     * @param geometry 图形对象
     */
    public void UpdateGeometry(Geometry geometry)
    {
        if(sketchEditor !=null && geometry!=null && !geometry.isEmpty())
        {
            sketchEditor.replaceGeometry(geometry);
            List<Point> points = GeometryUtils.geometryToPoints(geometry);
            if(points != null && points.size()>0)
            {
                SketchVertex sketchVertex = SketchVertex.createVertex(0, points.size()-1);
                sketchEditor.setSelectedVertex(sketchVertex);
            }
        }
    }

    /**
     * 通过绘画句柄判断是否可以回退来判断是否已经开始编辑
     * @return
     */
    public Boolean hasDraw()
    {
        if(sketchEditor!=null)
            return sketchEditor.canUndo();
        return  false;
    }
    /**
     * 获取绘制元素，在调用Clear 之前获取
     */
    public  Geometry GetGeometry()
    {
        if(sketchEditor!=null)
            return sketchEditor.getGeometry();

        return null;
    }

    public void Back()
    {
        if(sketchEditor!=null && sketchEditor.canUndo())
            sketchEditor.undo();
    }

    public void Forwad()
    {
        if(sketchEditor!=null && sketchEditor.canRedo())
            sketchEditor.redo();
    }

    private SketchCreationMode getSketchMode(DrawToolType drawToolType)
    {
        switch (drawToolType)
        {
            case Point:
                return  SketchCreationMode.POINT;
            case Polyline:
                return  SketchCreationMode.POLYLINE;
            case Polygon:
                return  SketchCreationMode.POLYGON;
            case FreePolyline:
                return  SketchCreationMode.FREEHAND_LINE;
            case FreePolygon:
                return  SketchCreationMode.FREEHAND_POLYGON;
        }
        return  null;
    }

    /**
     * 获取工具绘画类型
     * @return
     */
    public DrawToolType getDrawToolType()
    {
        return  drawToolType;
//        SketchCreationMode creationMode = sketchEditor.getSketchCreationMode();
//        if(creationMode == SketchCreationMode.POINT)
//        {
//            return  DrawToolType.Point;
//        }
//        else if(creationMode == SketchCreationMode.POLYLINE)
//        {
//            return  DrawToolType.Polyline;
//        }
//        else if(creationMode == SketchCreationMode.FREEHAND_LINE)
//        {
//            return  DrawToolType.FreePolyline;
//        }
//        else if(creationMode == SketchCreationMode.FREEHAND_POLYGON)
//        {
//            return  DrawToolType.FreePolygon;
//        }
//        return  null;
    }
}
